﻿using BUS;
using OnlineDigitalCameraWebsite.Class;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;

namespace OnlineDigitalCameraWebsite
{
    public partial class ThanhToan : System.Web.UI.Page
    {
        private Invoice _selectedInvoice = new Invoice();
        public Invoice SelectedInvoice
        {
            get
            {
                if (_selectedInvoice.InvoiceId == "")
                {
                    try
                    {
                        _selectedInvoice = (Invoice)Session["Invoice"];
                    }
                    catch { }
                }
                return _selectedInvoice;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (SelectedInvoice.InvoiceItems.Count == 0)
            {
                Response.Redirect("/GioHang.aspx");
            }
            //bool b = (bool)Session["DaDangNhap"];
            //if (b == true)
            //{
                if (!IsPostBack)
                {
                    LoadThongTin();
                }
            //}
            //else
            //{
            //    Response.Redirect("/DangNhap.aspx");
            //}
        }

        private void LoadThongTin()
        {
            UsersBUS uBUS = new UsersBUS(); 
            User u = new User();
            int idu = int.Parse(Session["UserID"].ToString());
            u = uBUS.GetByUserId(idu);
            if (u != null)
            {
                txtDienThoai.Text = u.userPhone;
                txtDiaChi.Text = u.userAddress;
                txtHoTen.Text = u.userName;
                txtEmail.Text = u.userEmail;
            }

        }

        private ArrayList _invoiceItems = new ArrayList();
        public ArrayList InvoiceItems
        {
            get
            {
                return _invoiceItems;
            }
            set
            {
                _invoiceItems = value;
            }
        }

        protected void btnCheckAfter_Click(object sender, EventArgs e)
        {
            //Page.Validate();

            //if (Page.IsValid)
            // {
            int userid = 1;
            int mao = 0;
            bool b = (bool)Session["DaDangNhap"];
            if (b == true)
            {

                userid = int.Parse(Session["UserID"].ToString());
            }
            else
            {
                // tao user voi email
                User u = new DAO.User();
                u.userAccount = txtEmail.Text;
                u.userPass = XuLy.MaHoaMatKhau("123456");
                u.userName = txtHoTen.Text;
                u.userEmail = txtEmail.Text;
                u.userPhone = txtDienThoai.Text;
                u.userAddress = txtDiaChi.Text;
                u.userStatus = true;
                u.roleId = 2;
                UsersBUS uBUS = new UsersBUS();
                uBUS.Insert(u);
                userid = u.userId;


            }
            // tao hoa don
            Order o = new Order();
            o.Dates = DateTime.Now;
            o.userId = userid;
            o.ordersStatus = false;
            o.Names = txtHoTen.Text;
            o.Addresses = txtDiaChi.Text;
            o.TotalAmount = Convert.ToDouble(SelectedInvoice.Total);
            o.Phone = txtDienThoai.Text;

            // chi tiet hoa don
            OrdersBUS orBUS = new OrdersBUS();
            orBUS.Insert(o);
            mao = o.ordersId;

            _invoiceItems = SelectedInvoice.InvoiceItems;

            foreach (InvoiceItem x in _invoiceItems)
            {
                double price = Convert.ToDouble(x.Price);
                // them vao chi tiet
                OrdersProductDetail op = new OrdersProductDetail();
                op.ordersId = mao;
                op.ProductId = x.ProductId;
                op.Price = price;
                op.Quantity = x.Quantity;
                op.Amount = price * x.Quantity;
                op.orderDetailsStatus = false;
                if (x.ProductCategory == "accessories")
                {
                    op.ProductType = 3;
                }
                else if (x.ProductCategory == "lens")
                {
                    op.ProductType = 2;
                }
                else
                {
                    op.ProductType = 1;
                }
                OrdersProductDetailBUS opBUS = new OrdersProductDetailBUS();
                opBUS.Insert(op);
            }
            SelectedInvoice.EmptyCart();
            Session["Invoice"] = SelectedInvoice;
            //redirect
            Response.Redirect("/Thank.aspx");

        }
        public void ShowMessage(string Content)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('" + Content + "');</script>");
        }
    }
}